package ipmss.services.common;

import ipmss.dao.authorities.UserDao;
import ipmss.dao.deskotop.DesktopDao;
import ipmss.dao.planning.ProjectDao;
import ipmss.entity.authorities.User;
import ipmss.entity.desktop.Desktop;
import ipmss.entity.planning.Project;
import ipmss.planning.ProjectProvider;

import java.util.List;
import java.util.logging.Logger;

import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;

// TODO: Auto-generated Javadoc
/**
 * The Class UserContextBean.
 */
@Named
@Scope(proxyMode = ScopedProxyMode.INTERFACES, value = "session")
public class UserContextBean implements UserContext
{
    
    /** The current project. */
    private Project currentProject;
    
    /** The current view. */
    private Desktop currentView;
    
    /** The projects. */
    private List<Project> projects;
    
    /** The views. */
    private List<Desktop> views;
    
    /** The Constant logger. */
    private static final Logger logger = Logger.getLogger(UserContextBean.class.getName());

    /** The user name. */
    private String userName;

    /** The user dao. */
    @Inject
    private UserDao userDao;

    /** The project dao. */
    @Inject
    private ProjectDao projectDao;
    
    /** The desktop dao. */
    @Inject
    private DesktopDao desktopDao;
    
    @Inject 
    private ProjectProvider projectProvider;

    /** The user. */
    private User user;

    /**
     * Gets the project by id.
     *
     * @param id the id
     * @return the project by id
     */
    public Project getProjectById(int id)
    {
	projects = getProjects();
	Project ret = null;
	for (Project p : projects)
	{
	    if (p.getId() == id)
	    {
		ret = p;
		break;
	    }
	}
	return ret;
    }
    
    /**
     * Instantiates a new user context bean.
     */
    public UserContextBean()
    {
    }

    /*
     * @PostConstruct public void postConstruc() { //
     * logger.info(ContextManager.getUserName()); logger.info("post construct");
     * }
     */

    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#getCurrentProject()
     */
    public Project getCurrentProject()
    {
	return currentProject;
    }

    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#setCurrentProject(ipmss.entity.planning.Project)
     */
    public void setCurrentProject(Project currentProject)
    {
	this.currentProject = currentProject;
    }

    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#getCurrentView()
     */
    public Desktop getCurrentView()
    {
	return currentView;
    }

    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#setCurrentView(ipmss.entity.desktop.Desktop)
     */
    public void setCurrentView(Desktop currentView)
    {
	this.currentView = currentView;
    }

    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#getProjects()
     */
    public List<Project> getProjects()
    {
	
	if(projects == null)
	    setProjects(projectDao.getUserProjects(getUser()));
	for(Project p : projects)
	{
	    p.setSchedule(projectProvider.getProjectById(p.getId()).getSchedule());
	}
	
	return projects;
    }

    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#setProjects(java.util.List)
     */
    public void setProjects(List<Project> projects)
    {
	this.projects = projects;
    }

    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#getViews()
     */
    public List<Desktop> getViews()
    {
	return views;
    }

    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#setViews(java.util.List)
     */
    public void setViews(List<Desktop> views)
    {
	this.views = views;
    }

    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#init(java.lang.String)
     */
    public void init(String userName)
    {

	logger.info("init start");
	userName = "michal.czarnik";
	this.userName = userName;

	User u = userDao.findUserByLogin(userName);
	user = u;
	initDesktop(u);
	initProjects(u);
	logger.info("init end");
	return;

    }
    
    /**
     * Inits the projects.
     *
     * @param u the u
     */
    private void initProjects(User u)
    {
     //  projects = projectDao.getUserProjects(u);
    }
    
    /**
     * Pre destroy.
     */
    @PreDestroy
    private void preDestroy()
    {
	if(ContextManager.getUserAuthentication() != null){
	    desktopDao.saveDesktop(currentView);
	    User u = userDao.findUserById(currentView.getUserId());
	    u.getSettings().setCurrentDesktopId(currentView.getId());
	    userDao.saveUser(u);
	}
    }

    /**
     * Inits the desktop.
     *
     * @param u the u
     */
    private void initDesktop(User u)
    {
	logger.info("initDesktop start");
	setViews(desktopDao.getUserDesktops(u.getId()));
	int currentDesktopId = u.getSettings().getCurrentDesktopId();
	views = desktopDao.getUserDesktops(u.getId());
	if (views.size() == 0 && currentDesktopId == -1)
	{
	    Desktop d = new Desktop();
	    d.setName("default");
	    d.setUserId(u.getId());
	    desktopDao.addNewDesktop(d);
	    currentDesktopId = d.getId();
	    views.add(d);
	    currentView = d;
	} else if (currentDesktopId == -1)
	{
	    currentView = views.get(0);
	    currentDesktopId = currentView.getId();
	    
	} else
	{

	    for (Desktop desktop : views)
	    {
		if (desktop.getId() == currentDesktopId)
		    currentView = desktop;
	    }
	}
	logger.info("initDesktop end");
    }
    
    /**
     * Change desktop.
     *
     * @param desktopName - new current desktop name
     * @return new current desktop
     */
    public Desktop changeDesktop(String desktopName)
    {
	logger.info("changeDesktop start");
	desktopDao.saveDesktop(currentView);
	for(Desktop d : views)
	{
	    if(d.getName().equals(desktopName))
	    {
		currentView = d;
		return currentView;
	    }
	}
	logger.info("changeDesktop end");
	return null;
    }
    

    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#getUserName()
     */
    public String getUserName()
    {
	// TODO Auto-generated method stub
	return userName;
    }




    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#getUser()
     */
    public User getUser()
    {
	return user;
    }

    /* (non-Javadoc)
     * @see ipmss.services.common.UserContext#getUserId()
     */
    public int getUserId()
    {
	return user.getId();
    }



}
